package com.janas.kktv;



import com.janas.kktv.tools.Toasting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ManualTaggingActivity extends Activity {
	
	public final String TAG = ManualTaggingActivity.class.getSimpleName();
	private final Context mContext = ManualTaggingActivity.this;
	private EditText metBarcodeView;
	private Button mbtnTagBarcode;		
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manual_tagging);	
		
		metBarcodeView = (EditText) findViewById(R.id.barcodeEditText);				
		mbtnTagBarcode = (Button) findViewById(R.id.tagBarcodeButton);
		mbtnTagBarcode.setOnClickListener( new TagBarcode() );		
	}
	
	
	private class TagBarcode implements OnClickListener {
		
		@Override
		public void onClick(View v) {
			
			boolean lLegalBarcode = checkManualInput(metBarcodeView.getText().toString());
			if (lLegalBarcode) {
				
				Toasting.inScreenCenter(mContext, "BARCODE OK");
			}
			Log.d( TAG, "TagBarcode" + "\nBARCODE: " + metBarcodeView.getText().toString());
		}
		
		
		private boolean checkManualInput(String pManualInput) {
			
			if ( pManualInput.length() != 13 ) {
				
				resetEditText();
				String lMessage = "Ihre Eingabe hat nicht exakt 13 Stellen";
				Toasting.inScreenCenter(mContext, lMessage);
				return false;
			}
			
			if ( !isDigit(pManualInput) ) {
				
				resetEditText();
				String lMessage = "Eingabe beinhaltet Illegale Zeichen. Nur Ziffern benutzen.";
				Toasting.inScreenCenter(mContext, lMessage);
				return false;
			}
			
			if ( !isLegalBarcode(pManualInput) ) {
				
				resetEditText();
				String lMessage = "Barcode ist ungültig";
				Toasting.inScreenCenter(mContext, lMessage);
				return false;
			}
			
			return true;
		}
		
		
		private boolean isDigit(String pManualInput) {
			
			int maxLength = 13;
			for (int i = 0; i < maxLength; i++) {
				
				char lDigit = pManualInput.charAt(i);
				if (!Character.isDigit(lDigit)) {
					
					return false;
				}
			}
			
			return true;
		}
		
		
		private boolean isLegalBarcode(String pBarcode) {
			
			boolean lPared = true;				
			int sum = 0, lastIndex = ( pBarcode.length() - 1 );	
					
			for ( int i = lastIndex - 1 ; i >= 0; --i ) {
				
				int lValue = Integer.valueOf(pBarcode.substring( i, (i+1) ));			
				if (lPared) {
					
					sum += lValue * 3;
					lPared = false;				
				} else {
					
					sum += lValue;
					lPared = true;
				}
			}
			
			sum %= 10;			
			int lCheckSum = sum == 0 ? 0 : 10 - sum;
			int lCheckValue = Integer.valueOf(pBarcode.substring(lastIndex));
			
			if (lCheckSum == lCheckValue) {
				
				return true;
			} else {
				
				return false;
			}
		}
		
		
		private void resetEditText() {
			
			metBarcodeView.setText("");	
		}
	}

}
